<?php
session_start();
//check if the session 'estado' is a teacher
if ( $_SESSION['estado'] == "teacher_login" ) {


	//it runs after pressing the submit button
	if($_SERVER['REQUEST_METHOD'] == 'POST'){

		//if is not empty the select,it runs this 
		if (!empty($_POST['groups'])){
		
		$selected_group_name = $_POST['groups'];
		$_SESSION['group'] = $selected_group_name;
		
		//include header
		include("header.php");
		
		echo "<div id='login_div2'>";
		echo "<br />";
		
		//after selecting the group, print a message of the selected group
		echo "<br /><br /><h1>The selected group is: <b>". $_SESSION['group']."</b></h1>";
		echo "<br /><img src='good.png' alt=''><br />";
		
		}
		else
		{	
			//include header
			include("header.php");
			//if you sumbit the form and not select a group, print an error message
			echo "<p style='color: red;'>Please, choose a group before continuing.</p>";
			echo "<br /><a href='ChooseGroup.php'>Return to Choose Group</a><br /><br />";
			
		}
		//close the div
		echo "<br /><br /></div><br />";
	
	}
	else
	{	
		//include the header
		include("header.php");
		//have the settings for connect to the database
		require('mysqlsettings.php');

		// Attempt to connect to MySQL and print out messages:
		$dbc = @mysql_connect($server, $username, $password);
		mysql_set_charset('utf8',$dbc);
		mysql_select_db('my_marks', $dbc);
				
		//save the query into a variable
		$query = 'SELECT * FROM groups ORDER BY ID ASC';
		
		//print div with a background image
		echo "<div id='login_div2'>";
		echo "<br /><br />";
		
		//mount the select of the groups and runs a loop while the query is running correctly
		if ($r = mysql_query($query, $dbc)) { 
			echo "<h1>Choose Group</h1>";
			echo "<form action='' method='POST'>";
			echo "<select name='groups'>";
			echo "<option></option>";
			while ($row = mysql_fetch_array($r)) {
			echo "<option value='{$row['GroupName']}'>{$row['GroupName']}</option>";
			}	
			echo "</select>";
			echo "<input type='submit' value='Choose' />";
			//close the form of select
			echo "</form><br /><br />";

		}
		//close the div
		echo "<br /><br /></div><br />";
		
	}


}
//check if the session 'estado' is a student
else if ( $_SESSION['estado'] == "student_login" ) {
//if the sesion 'estado' is a student, include a header of the students that have permision denied for enter at this page
include("headerstudent.php");

}

//if you don't have a session started, you can't enter at this page
else {

include("noacces.php");

}
//include the footer
include("footer.php");

?>